from django.shortcuts import render
from django.views import View
from .models import Area
from django import http
from django.core.cache import cache

# Create your views here.


class ArearView(View):
    """省市区三级联动"""

    def get(self, request):
        # 判断当前是要查询省份数据还是市区数据
        area_id = request.GET.get('area_id')
        if not area_id:
            # 判断是否有缓存数据
            province_list = cache.get('province_list')
            if not province_list:
                # 提供省份数据
                try:
                    # 查询省份数据
                    province_model_list = Area.objects.filter(parent__isnull=True)
                    # 序列化省级数据
                    province_list = []
                    for province_model in province_model_list:
                        province_list.append({'id': province_model.id, 'name': province_model.name})
                    # print(province_list)

                    # 缓存省份列表数据  (默认存储到default数据库)
                    cache.set('province_list', province_list, 3600)

                except Exception as e:
                    return http.JsonResponse({'code': '4000', 'errmsg': '省份数据错误'})
            # 响应省份数据
            return http.JsonResponse({'code': '0', 'errmsg': 'OK', 'province_list': province_list})
        else:
            # 读取市或区缓存数据
            sub_data = cache.get('sub_area_' + area_id)
            if not sub_data:
                # 提供市或区数据
                try:
                    """
                                    {
                      "code":"0",
                      "errmsg":"OK",
                      "sub_data":{
                          "id":130000,
                          "name":"河北省",
                          "subs":[
                              {
                                  "id":130100,
                                  "name":"石家庄市"
                              },
                              ......
                          ]
                      }
                    }
                    """
                    parent_model = Area.objects.get(id=area_id)  # 查询市或区的父级
                    sub_model_list = parent_model.subs.all()  # 查询子集

                    # 序列化市或区数据
                    sub_list = []
                    for sub_model in sub_model_list:
                        sub_list.append({
                            'id': sub_model.id,
                            'name': sub_model.name
                        })

                    sub_data = {
                        'id': parent_model.id,  # 父级id
                        'name': parent_model.name,  # 父级name
                        'subs': sub_list  # 父级的子集
                    }
                    # print(sub_data)
                    # 储存市或区缓存数据
                    cache.set('sub_area_' + area_id, sub_data, 3600)
                except Exception as e:
                    return http.JsonResponse({'code': '4000', 'errmsg': '城市或区数据错误'})
            # 响应市或区数据
            return http.JsonResponse({'code': '0', 'errmsg': 'OK', 'sub_data': sub_data})


